<?php

namespace app\crontab\task;

use app\common\library\task\ImportService;
use app\common\library\task\RateService;
use app\common\model\zbtask\Import;
use app\common\model\zbtask\Money;
use app\common\model\zbtask\Rate;
use app\common\model\zbtask\Zbtask;
use think\Db;


/**
 * 计划的定时任务 
 */ 
class TestMoney extends Common
{
    public function check() {
        $where['company_id'] = 131;
        $list = Money::where($where)->whereTime('createtime', 'last month')->select();
        foreach ($list as $k => $v) {
            $where['task_money_id'] = $v['id'];
            $count = Db::name('zbtask_money_rate')->where($where)->count();
            if ($count == 0) {
                echo $v['id'];die;
            }
        }
        echo 'ok';exit;
    }


    /**修正程序*/
    public function fixRate() {
        Money::whereTime('createtime', 'last month')->chunk(1000, function($items){
            foreach($items as $item){
                Rate::where(['task_money_id'=> $item['id']])->setField('company_id', $item['company_id']);
            }
        });

        return true;
    }


    /**修复众包费*/
    public function fixMoney() {
        $import = new Import();
        $import->where(['status'=> 1, 'is_import' => 0])->chunk(1, function($items) use ($import){
            foreach($items as $item){
                //生成税率
                Db::startTrans();
                try {
                    $data = $item->toArray();
                    //对导入的数据惊醒
                    ImportService::instance()->setData($item)->createUser()->createOrder()->insertData();
                    //导入成功 修改导入 状态
                    $import->where(['id'=> $item['id']])->setField('is_import', 1);
                    var_export($item->toArray());exit;

                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                }
            }
        });

        return true;
    }
}